#!/bin/bash

USAGE="Usage: ${0} [m|u|l] [targets]
    m Mount all volumes indicated in ~/Remotes/
    u Unmount all volumes indicated in ~/Remotes/
    l List all targets and whether they're mounted or not.

    If targets are supplied after u or m only those will
    be mounted/unmounted."

cd ~/Remotes

if [[ ${1} == "l" ]]; then
	for remote in *; do
		STATUS="UNMOUNTED:"
		if grep "${PWD}/${remote}" /etc/mtab &> /dev/null; then
			STATUS="MOUNTED:  "
		fi
		echo "${STATUS} ${remote}"
	done
elif [[ ${1} == "m" ]]; then
	shift
	REMOTES="${@:-*}"
	for remote in ${REMOTES}; do
		if ! grep "${PWD}/${remote}" /etc/mtab &> /dev/null; then
			sshfs ${remote}:. ${remote}
		fi
	done
elif [[ ${1} == "u" ]]; then
	shift
	REMOTES="${@:-*}"
	for remote in ${REMOTES}; do
		if grep "${PWD}/${remote}" /etc/mtab &> /dev/null; then
			fusermount3 -u ${remote}
		fi
	done
else
	echo "${USAGE}"
fi

